/*
 * $Id: Plugin.java 53 2009-04-12 10:36:20Z meyer@alphatec.de $
 * 
 *
 * Copyright 2009 DeltaDesk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package deltadesk.gui.deltagui;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import deltadesk.gui.deltagui.app.ExitHandler;
import deltadesk.gui.deltagui.app.RootPane;
import deltadesk.gui.deltagui.app.platform.MacOSXAppHandler;
import deltadesk.gui.deltagui.widget.Desktop;
import deltadesk.gui.deltagui.widget.Panel;
import deltadesk.gui.deltagui.widget.Picture;

/**
 * Deltadesk DeltaGUI plugin.
 * 
 * @author Andreas Meyer
 * @version $Revision: 53 $
 */
public final class Plugin extends org.java.plugin.Plugin implements ExitHandler {

    /** This plug-in ID. */
    public static final String PLUGIN_ID = "deltadesk.gui.deltagui";
    
	/** The aplication default name. */
	private static final String DEFAULT_NAME = "DeltaDesk Basic Application";

	/** Our main window. */
	private JFrame frame;

	/** The device for fullscreen. */
	private GraphicsDevice device;

	/** The original display mode. */
	private DisplayMode originalDisplayMode;

	/** The root pane. */
	private RootPane rootPane;


    /**
     * @see org.java.plugin.Plugin#doStart()
     */
    protected void doStart() throws Exception {
        log.info("+DeltaDesk.DeltaGUI");
        
		// create main window
		frame = new JFrame();
		frame.setBackground(Color.BLACK);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

		final String osName = System.getProperty("os.name");
		log.info(" Running on " + osName);

		// handle Macintosh OS X specific stuff
		if (osName.contains("Mac")) {
			MacOSXAppHandler.handle(this);
		}

		//TODO: resolve main control for the app
		final Panel p = new Panel(4);
		final Picture pic = new Picture(
				"http://www.google.de/intl/de_de/images/logo.gif");
		p.add(pic);
		p.add("Lorem ipsum dolor sit amet, consectetuer sadipscing elitr, sed " +
				"diam nonumy eirmod tempor");
		
		rootPane = new RootPane(this, p);
		
		//TODO: make configurable
		rootPane.getDesktop().setShowFPS(true);

		launch();
		log.info(" Using " + rootPane.getDesiredFPS() + " FPS");

        log.info("*DeltaDesk.DeltaGUI");
    }

	/**
	 * Launch the application .
	 */
	public void launch() {
		launch(0, DEFAULT_NAME);
	}

	public void launch(final String name) {
		launch(0, name);
	}

	/**
	 * Launch the application on a desired display.
	 * 
	 * @param displayNumber
	 * @param name
	 */
	public void launch(final int displayNumber, final String name) {

		// resolve screen devices
		final GraphicsEnvironment ge = GraphicsEnvironment
				.getLocalGraphicsEnvironment();
		final GraphicsDevice[] devices = ge.getScreenDevices();
		if (displayNumber >= devices.length) {
			log.error("Display " + displayNumber + " not found");
			System.exit(1);
		}

		final Thread t = new Thread(rootPane);
		t.start();

		// launch in fullscreen mode
		device = devices[displayNumber];
		originalDisplayMode = device.getDisplayMode();
		frame.setName(name);

		frame.setContentPane(rootPane);
		frame.setUndecorated(true);
		device.setFullScreenWindow(frame);

		// create page flipping buffer
		frame.createBufferStrategy(2);
		final BufferStrategy bs = frame.getBufferStrategy();
		rootPane.setBufferStrategy(bs);
	}

	/**
	 * Return the desktop.
	 * 
	 * @return
	 */
	public Desktop getDesktop() {
		return rootPane.getDesktop();
	}

	/**
	 * @return the rootPane
	 */
	public RootPane getRootPane() {
		return rootPane;
	}

	/**
	 * Set the desired frames per second.
	 * 
	 * @param fps
	 */
	public void setFPS(final int fps) {
		rootPane.setDesiredFPS(fps);
	}

    /**
	 * Set a new display mode.
	 * 
	 * @param dm
	 * @return
	 */
	public boolean setDisplayMode(final DisplayMode dm) {

		try {

			device.setDisplayMode(dm);
			rootPane.showCursor(false);
			getDesktop().setSize(dm.getWidth(), dm.getHeight());
			getDesktop().invalidate();
			getDesktop().setUnrendered();
		} catch (Exception e) {
			return false;
		}

		return true;
	}

    /**
     * Switch back to the original display mode and exit.
     * 
     * @see org.java.plugin.Plugin#doStop()
     */
    protected void doStop() throws Exception {
        log.info("-DeltaDesk.DeltaGUI");

		rootPane.setRunning(false);
		device.setDisplayMode(originalDisplayMode);
		frame.dispose();

        log.info("_DeltaDesk.DeltaGUI");
    }
    
	/**
	 * Shut down JPF
	 */
	public void exit() {
		getManager().shutdown();
	}

}
